package all.com.jinyongqunxiax.model.mod;

import com.randy.fundation.JxAssert;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import all.com.jinyongqunxiax.BuildConfig;
import androidx.annotation.NonNull;
import jy.game.components.map.JyMap;
import jy.game.tools.BasePojoManager;

public class MapManager {
    public Map<String, JyMap> map  = new HashMap<>();
    public List<JyMap>        list = new ArrayList<>();
    String xmlPath;

    public MapManager(@NonNull String xmlPath) {
        this.xmlPath = xmlPath;
    }

    BasePojoManager<JyMap> manager;

    public String load() {
        map.clear();
        list.clear();

        manager = BasePojoManager.build(xmlPath, JyMap.class);
        list = manager.datas;
        if (!manager.success()) {
            return manager.message;
        }
        StringBuilder message = new StringBuilder(getClass().getSimpleName());

        for (JyMap item : list) {
            if (map.containsKey(item.name)) {
                message.append("发现重复:").append(item.name);
                continue;
            }
            map.put(item.name, item);
        }


        return manager.message;
    }
//
//    public String load() {
//        try {
//            map.clear();
//            list.clear();
//
//            Serializer serializer = new Persister();
//
//            File file = new File(xmlPath);
//            Root root = serializer.read(Root.class, file);
//            assert !BuildConfig.DEBUG || !root.models.isEmpty();
//
//            list = root.models;
//            for (Model model : list) {
//                map.put(model.name, model);
//            }
//
//            if (BuildConfig.DEBUG) {
//                assert map.entrySet().size() == list.size();
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            return e.getMessage();
//        }
//        return "";
//    }
//
//
//    static public class Root {
//        @ElementList(inline = true)
//        List<Model> models;
//    }
//
//    /**
//     * <map desc="有人的地方，就是江湖！" name="大地图" pic="地图.大地图">
//     * <mapunit description="昆仑山延绵千里，白雪皑皑，流传着无数关于她的神奇故事。" name="昆仑山" pic="" x="112" y="208">
//     * <event description="" image="town.waiguo4" probability="100" repeat="" type="map" value="昆仑山"/>
//     * </mapunit>
//     */
//    @ wx.xml.simpleframework.xml.Root(name = "map")
//    static public class Model {
//        public @Attribute                                                      String        name;
//        public @Attribute(name = "pic")                                        String        icon;
//        public @Attribute                                                      String        desc;
//
//        public @ElementList(name = "mapunit", inline = true, required = false) List<MapUnit> mapUnits;
//        public @ElementList(name = "musics", inline = true, required = false) List<Musics> musics;
//    }
//
//    /**
//     * <music name="音乐.武侠进行"/>
//     */
//    @ wx.xml.simpleframework.xml.Root(name = "musics")
//    static public class Musics {
//        public @ElementList(inline = true, required = false) List<Music> musics;
//    }
//
//    /**
//     * <music name="音乐.武侠进行"/>
//     */
//    @ wx.xml.simpleframework.xml.Root(name = "music")
//    static public class Music {
//        public @Attribute String name;
//    }
//
//    /**
//     * <mapunit description="昆仑山延绵千里，白雪皑皑，流传着无数关于她的神奇故事。" name="昆仑山" pic="" x="112" y="208">
//     * <event description="" image="town.waiguo4" probability="100" repeat="" type="map" value="昆仑山"/>
//     * </mapunit>
//     */
//    @ wx.xml.simpleframework.xml.Root(name = "mapunit")
//    static public class MapUnit {
//        public @Attribute                                    String      name;
//        public @Attribute                                    String      description;
//        public @Attribute(name = "pic")                      String      icon;
//        public @Attribute                                    int         x;
//        public @Attribute                                    int         y;
//        public @ElementList(inline = true, required = false) List<Event> event;
//    }
//
//    /**
//     * <event description="" image="town.waiguo4" probability="100" repeat="" type="map" value="昆仑山"/>
//     */
//    @ wx.xml.simpleframework.xml.Root(name = "event")
//    static public class Event {
//        public @Attribute String description;
//        public @Attribute String image;
//
//        public @Attribute String probability;
//
//        public @Attribute String repeat;
//
//        public @Attribute String type;
//        public @Attribute String value;
//
//        public   @ElementList(inline = true, required = false) List<JyCondition> conditions = new ArrayList<>();
//    }
//
//    /**
//     * <condition type="in_team" value="李筱筠"/>
//     */
//    @ wx.xml.simpleframework.xml.Root(name = "condition")
//    static public class JyCondition {
//        public @Attribute String type;
//        public @Attribute String value;
//    }
}
